﻿CloudDisk.Admin.NotificationPanel = (function(cfg){
    var el = $('#notificationPanel');
	
	var notificationItemTpl = $('#notificationItemTpl');
	
	var members = [];
	
	var initialized = false;
	
    return {
	    init: function(){
		    var that = this;
		    el.find('.adm-member-toolbar button').click(function(){
			    CloudDisk.Admin.SendNotificationWindow.open(function(o){			
					CloudDisk.Admin.SessionMgr.sendNotification(o, function(){			    

					});
				});
			});
			
			el.find('.search-bar input').keyup(function(){
			    var filteredMembers = [];
				var key = $(this).val();
				var body = el.find('.adm-member-body');
				for(var i = 0; i < members.length; ++i){
				    var m = members[i];
					if(m.user_name.indexOf(key) > -1 || m.email.indexOf(key) > -1){
					    filteredMembers.push(m);
					}
				}
				body.empty();
				for(var i = 0; i < filteredMembers.length; ++i){
				    body.append(that.createItem(filteredMembers[i]));
				}
			});
		},
		
		activate: function(){
		    var that = this;
		    el.show();
			if(!initialized){
			    that.init();
				initialized = true;
			}
			
			CloudDisk.Admin.SessionMgr.queryReceivedNotifications(function(data){			    
				var body = el.find('.adm-member-body');
				members = data.result;
				
				body.empty();
				for(var i = 0; i < members.length; ++i){
				    body.append(that.createItem(members[i]));
				}
			});
		},
		
		createItem: function(data){
			var item = $('<div class="adm-member-item notification-item"></div>');
			var that = this;
			item.html(notificationItemTpl.render(data));
			item.attr('data-id', data.id);
			
			if(data.is_read == '0'){
			    item.addClass('unread');
			}
			
			item.on('click', function(e){
				var tar = e.target;
				var itemId = $(this).attr('data-id');
				if(tar.getAttribute('action') == 'reply'){
					CloudDisk.Admin.ReplyNotificationWindow.open(function(o){
					    CloudDisk.Admin.SessionMgr.sendNotification({
						    toUserId: item.find('.col-from').attr('data-id') + ';',
							toUserName: item.find('.col-from').html() + ';',
							title: '回复:' + item.find('.col-title').html(),
							content: o.content
						}, function(){			    
							//item.attr('data-id')
						});
					});
				} else if(tar.getAttribute('action') == 'delete'){
					CloudDisk.Admin.SessionMgr.deleteNotification({
					    id: itemId
					}, function(){			    

					});
				} else {
				    item.toggleClass('expanded');
					if(item.hasClass('unread')){
						item.removeClass('unread');
						CloudDisk.Admin.SessionMgr.readNotification({
							id: itemId
						}, function(){			    

						});
					}
				}
			});
			return item;
		}
	};
})();

CloudDisk.Admin.SendNotificationWindow = (function(){
    var tpl = [
	    '<div class="error-message"></div>',
		'<div style="float:left;padding:20px 0 20px;">',
		    '<div>选择接收人：</div>',
			'<div class="user-list"></div>',
		'</div>',
	    '<div class="" style="float: right; width: auto; min-height: 0px; height: 431px;padding:20px;">',		    
            '<div class="form-item">', 
			    '<label>接收人:</label>',
				'<input name="receiverIds" type="hidden">',
				'<input name="receiverNames" type="text" class="input-text" readonly="true">',
			'</div>',
			'<div class="form-item">',
			    '<label>标题:</label>',
				'<input name="title" type="text" class="input-text">',
			'</div>',
			'<div class="form-item">',
			    '<label>内容:</label>',
				'<textarea name="content" rows="10" class="input-text"></textarea>',
			'</div>',			
            '<div class="add-member-ops" style="margin-top:20px;">',
			    '<button action="send" class="primary-btn">确定</button>',
				'<button action="cancel" class="secondary-btn">取消</button>',
			'</div>',
		'</div>'].join('');
	
	var itemTpl = $.templates([
			'<label><input type="checkbox" value="{{:user_id}}" data-content="{{:user_name}}">{{:user_name}}</label>'
	].join(''));
	
	var members = [];
	
	var selected = [];
	
	return new CloudDisk.Window({
	    title: '发送消息',
		content: tpl,
		width: 600,
		height: 230,
		open: function(callback){
		    this.callback = callback;
	        CloudDisk.Util.showMask();
			this.el.show();
			this.position();
			
			var ct = this.el.find('.user-list');
			var that =this;
			ct.empty();
			ct.html('加载中...');
			CloudDisk.Admin.SessionMgr.queryUserList(function(data){	
                ct.empty();		    
                members = data.result;
				for(var i = 0; i < members.length; ++i){
				    ct.append(that.createMemberItem(members[i]));
				}
			});

			that.el.find('.error-message').hide();
			that.el.find('input[name=receiverIds]').val('');
			that.el.find('input[name=receiverNames]').val('');
			that.el.find('input[name=title]').val('');
            that.el.find('textarea[name=content]').val('');			
		},
		
		createMemberItem: function(data){
			var item = $('<div></div>');
			var that = this;
			item.html(itemTpl.render(data));
			item.attr('member-id', data.user_id);
			
			item.first('input').on('change', function(e){
				var boxes = that.el.find('.user-list input');
				var ids = '', names = '';
				for(var i = 0; i < boxes.length; ++i){
				    if(boxes[i].checked){
					    ids += boxes[i].value + ';'
						names += boxes[i].getAttribute('data-content') + ';'
					}
				}
				that.el.find('input[name=receiverIds]').val(ids);
			    that.el.find('input[name=receiverNames]').val(names);
			});
			return item;
		},
		
		clickHandler: function(e, o){
		    var tar = e.target;
			if(tar.getAttribute('action') == 'send'){
			    var receiverIds = o.el.find('input[name=receiverIds]').val();
				var receiverNames = o.el.find('input[name=receiverNames]').val();
				var title = o.el.find('input[name=title]').val();
				var content = o.el.find('textarea[name=content]').val();
				
				if(!receiverIds){
				    o.el.find('.error-message').html('接收人不能为空').show();
					return;
				} else if(!title){
				    o.el.find('.error-message').html('标题不能为空').show();
					return;
				} if(!content){
				    o.el.find('.error-message').html('内容不能为空').show();
					return;
				}
				
			    o.callback({
					receiverIds: receiverIds,
					receiverNames: receiverNames,
					title: title,
					content: content
				});
			    o.close();
			} else if(tar.getAttribute('action') == 'cancel'){
			    o.close();
			}
		}
	});
})();

CloudDisk.Admin.ReplyNotificationWindow = (function(){
    var tpl = [
	    '<div class="error-message"></div>',
	    '<div class="" style="width: auto; min-height: 0px;padding:20px;">',
			'<div class="form-item">',
				'<textarea name="content" rows="6" class="input-text" style="width:400px;"></textarea>',
			'</div>',			
            '<div class="add-member-ops" style="margin-top:20px;">',
			    '<button action="send" class="primary-btn">确定</button>',
				'<button action="cancel" class="secondary-btn">取消</button>',
			'</div>',
		'</div>'].join('');
	
	return new CloudDisk.Window({
	    title: '回复',
		content: tpl,
        width: 500,
		height: 280,
		
		clickHandler: function(e, o){
		    var tar = e.target;
			if(tar.getAttribute('action') == 'send'){
				var content = o.el.find('textarea[name=content]').val();
				if(!content){
				    o.el.find('.error-message').html('内容不能为空').show();
					return;
				}
				
			    o.callback({
					content: content
				});
			    o.close();
			} else if(tar.getAttribute('action') == 'cancel'){
			    o.close();
			}
		}
	});
})();

